package ruifeng.zcj.mybase.base.base_web_activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jakewharton.rxbinding.view.RxView;
import com.rey.material.widget.ProgressView;

import ruifeng.zcj.mybase.R;
import ruifeng.zcj.mybase.base.App;
import ruifeng.zcj.mybase.base.BaseActivity;
import ruifeng.zcj.mybase.base.base_web_activity.other.JS;

/**
 * Created by Administrator on 2016/8/23 0023.
 */
public abstract class WebViewActivity extends BaseActivity implements
        SwipeRefreshLayout.OnRefreshListener {
    public static final String KEY_CLASS_NAME = "className";
    public static final String KEY_ARGS = "args";

    LinearLayout layoutLoading;
    LinearLayout mLayoutContent;
    LinearLayout layoutLoadFailed;
    LinearLayout layoutEmpty;
    ProgressView mProgress;
    public WebView mWebview;
    TextView mTxtLoadFailed;
    TextView mTxtLoadEmpty;
    TextView mTxtLayoutReload;
    SwipeRefreshLayout swipeRefreshLayout;

    public WebSettings ws;
    public String MyURL;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = View.inflate(App.getAppContext(), R.layout.activity_main, null);
        layoutLoading = (LinearLayout) view.findViewById(R.id.layoutLoading);
        mLayoutContent = (LinearLayout) view.findViewById(R.id.layoutContent);
        layoutLoadFailed = (LinearLayout) view.findViewById(R.id.layoutContent);
        layoutEmpty = (LinearLayout) view.findViewById(R.id.layoutContent);
        mProgress = (ProgressView) view.findViewById(R.id.layoutContent);
        mWebview = (WebView) view.findViewById(R.id.layoutContent);
        mTxtLoadFailed = (TextView) view.findViewById(R.id.layoutContent);
        mTxtLoadEmpty = (TextView) view.findViewById(R.id.layoutContent);
        mTxtLayoutReload = (TextView) view.findViewById(R.id.layoutContent);
        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.layoutContent);
        setContentView(view);
    }
    @Override
    public int getLayoutId() {
        return 0;
    }

    public interface TextViewChanger {
        void changeText(String arg);
    }

    private TextViewChanger mTextChanger = new TextViewChanger() {// 回调方法
        @Override
        public void changeText(final String arg) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    pay(arg);
                }
            });
        }
    };

    // js代码调用java代码之后该做什么事情做什么事情
    public void pay(String arg) {

    }

    @Override
    public void initView() {

        ws = mWebview.getSettings();
        ws.setJavaScriptEnabled(true);
        ws.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);  //设置 缓存模式(true);
        ws.setAppCacheEnabled(true);
        ws.setSupportZoom(false);
        ws.setUseWideViewPort(true);// 可任意比例缩放
        ws.setJavaScriptCanOpenWindowsAutomatically(true);// js支持
        ws.setDomStorageEnabled(true);

        mWebview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return WebViewActivity.this.shouldOverrideUrlLoading(view, url);
            }

            /***
             * 判断有网络情况下，页面出错误了才会调用这个方法
             * */
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                if (errorCode == -2) {
                    layoutLoadFailedVisibility(true);
                    Toast.makeText(App.getAppContext(), "网络连接失败 ,请连接网络。", Toast.LENGTH_SHORT).show();
                    view.setVisibility(View.INVISIBLE);
                } else {// 失败-10
                    layoutEmptyVisibility(true);
                    Toast.makeText(App.getAppContext(), "网速较慢 ,请刷新。", Toast.LENGTH_SHORT).show();
                }
            }


            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                if (mProgress != null) {
                    mProgress.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if (mProgress != null) {
                    mProgress.setVisibility(View.GONE);
                }
                if (isRefreshing()) {
                    hideSwipeRefreshing();
                }
                layoutLoadingVisibility(false);
            }
        });
        mWebview.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress < 100) {
                    if (mProgress != null) {
                        mProgress.setVisibility(View.VISIBLE);
                    }

                } else if (newProgress == 100) {
                    if (mProgress != null) {
                        mProgress.setVisibility(View.GONE);
                    }
                }
                if (mProgress != null) {
                    mProgress.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }
        });

        swipeRefreshLayout.setOnRefreshListener(this);
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);

        if (isAddJavascriptInterface()) {
            AddJavascript();
        }
        mWebview.loadUrl(getURL());
    }
    public String getURL(){
        if (!TextUtils.isEmpty(MyURL)) {
            return MyURL;
        }else {
            layoutLoadFailedVisibility(false);
            layoutEmptyVisibility(true);
            layoutContentVisibility(false);
            layoutLoadingVisibility(false);
        }
        return null;
    }

    // 是否调用js代码默认是传递
    protected boolean isAddJavascriptInterface() {
        return false;
    }

    // 传递一个类给html
    public void AddJavascript() {
        JS js = newJsObj();
        js.setTextChanger(mTextChanger);// 把接口发送过去,之后会回调,js调java
        mWebview.addJavascriptInterface(js, HTMLkey());
        // JS js = TUtil.getT(this, 0);
        // js.setTextChanger(mTextChanger);// 把接口发送过去,之后会回调,js调java
//        mWebview.addJavascriptInterface(js,"webview");
    }

    // new一个子类
    public JS newJsObj() {// 传递一个子类给
        return null;
    }


    // 一个key
    public String HTMLkey() {// 获取他的key
        return "Android";
    }



    public abstract boolean shouldOverrideUrlLoading(WebView view, String url);


    public void layoutLoadingVisibility(boolean isVisible) {
        if (layoutLoading != null) {
            RxView.visibility(layoutLoading, View.GONE).call(isVisible);
        }
    }

    public void layoutContentVisibility(boolean isVisible) {
        if (mLayoutContent != null) {
            RxView.visibility(mLayoutContent, View.GONE).call(isVisible);
        }
    }

    public void layoutLoadFailedVisibility(boolean isVisible) {
        if (layoutLoadFailed != null) {
            RxView.visibility(layoutLoadFailed, View.GONE).call(isVisible);
        }
    }

    public void layoutEmptyVisibility(boolean isVisible) {
        if (layoutEmpty != null) {
            RxView.visibility(layoutEmpty, View.GONE).call(isVisible);
        }
    }

    public void showSwipeRefreshing() {
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setRefreshing(true);
        }
    }

    public void hideSwipeRefreshing() {
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setRefreshing(false);
        }
    }

    public boolean isRefreshing() {
        if (swipeRefreshLayout != null) {
            return swipeRefreshLayout.isRefreshing();
        }
        return false;
    }


    @Override
    public void onRefresh() {
        if (mWebview != null) {
            mWebview.reload();
            layoutLoadFailedVisibility(false);
            layoutEmptyVisibility(false);
            layoutContentVisibility(true);
            layoutLoadingVisibility(true);
            mWebview.setVisibility(View.VISIBLE);
        }
    }


}
